package com.kitty.game.fight.pet;

import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.utils.StatusConst;

/**
 * 法力护盾
 * 对自己使用成功后，可以通过消耗法力来抵御受到的伤害。技能等级越高，每点法力可抵御的伤害越高
 */
public class FaLiHuDunProcessor extends PetSkillProcessor {

    @Override
    public void trigger(Fight fight, FightObject effector, FightObject target, int skillLevel, Object... args) {
        // 只能对自己
        if (effector != target) {
            return;
        }

        int status = bindingStatus();
        double rate = skillLevel * 0.005;
        int shield = (int) (effector.getCurrMana() * rate);
        if (shield > 0) {
            effector.setStatus(status, shield);
            effector.reduceMp(shield);
            FightMessageService fightMessageService = SpringUtils.getFightMessageService();
            FightMessageUtil.sendFightPackage(fight, fightMessageService.decreaseMana(effector.getId(), effector.getId(), shield, 0));
            FightMessageUtil.sendFightPackage(fight, fightMessageService.updateObjectInfo(effector));
        }
    }

    @Override
    public Integer effect(Fight fight, FightObject effector, FightObject target, Object... args) {
        int hurt = (int) args[0];
        if (fight.isSameSide(effector, target)) {
            return hurt;
        }

        int status = bindingStatus();
        int shield = target.getStatusScore(status);
        if (shield <= 0) {
            return hurt;
        }
        if (hurt <= shield) {
            shield -= hurt;
            hurt = 0;
            target.setStatusScore(status, shield);
        } else {
            hurt -= shield;
            target.setStatusScore(status, 0);
        }
        return hurt;
    }

    public int bindingStatus() {
        return StatusConst.STATUS_MANA_SHIELD;
    }
}
